
//
//  LFLocation.m
//  QingDaoCommunity
//
//  Created by 李学林 on 15/11/23.
//  Copyright © 2015年 teteTechnology. All rights reserved.
//

#import "LFLocation.h"


@interface LFLocation ()<CLLocationManagerDelegate>

@property(nonatomic,strong)CLLocationManager *manager;

@property(nonatomic,copy)successLocal sLocal;
@property(nonatomic,copy)failledLocal eLocal;



//添加一个标志用于记录回调
@property(nonatomic,assign)BOOL local;

@end

@implementation LFLocation

+ (instancetype)sharedManager
{
    static id _instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    
    return _instance;
}


#pragma mark------定位------
-(instancetype)init{

    if (self = [super init]) {
        
        _manager = [[CLLocationManager alloc]init];
        
        //ios8 需要授权
        if ([_manager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            
            [_manager requestAlwaysAuthorization];
        }
        
        
        
        _manager.delegate = self;
        
        
    }

    return self;

}





#pragma mark----CLLocationManagerDelegate代理

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    
    //    [mana ger stopUpdatingLocation];
    
    NSLog(@"hello4");

    self.eLocal(error);
}


- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    
    
    if (locations>0) {
        
        CLLocation *location = locations.lastObject;
        
        if (!_local) {
            
            self.sLocal(location.coordinate);
            
            _local = YES;
        }
    }
    
    
    
    
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    
    if (status ==kCLAuthorizationStatusDenied ) {
        
        [manager stopUpdatingLocation];
    }
    
    
}

// 定位成功或者失败之后block传递数据
+ (void)locationSuccess:(successLocal)success orFail:(failledLocal)error{

    DSLog(@"hello2");

    [[LFLocation sharedManager] locationSuccess:success orFail:error];
    


}
- (void)locationSuccess:(successLocal)success orFail:(failledLocal)error{
    

    
    //开始定位
    [_manager startUpdatingLocation];

    _local = NO;
    
    self.sLocal = success;
    
    self.eLocal = error;
    
}


@end
